<?php

namespace app\common\service;


class HostSoftware extends Base 
{
    /**
     * @name 列表
     * @param $params
     * @param $limit
     * @return array
     * @author wx
     * @date time
     */
    function list($params, $limit)
    {
        $db = db('host_software');

        if(isset($params['name']) && !empty($params['name'])){
            $db->where('name','like','%'.$params['name'].'%');
        }
        if(isset($params['status']) && !empty($params['status'])){
            $db->where('status',$params['status']);
        }
        if(isset($params['description']) && !empty($params['description'])){
            $db->where('description','like','%'.$params['description'].'%');
        }
        if(isset($params['on_time']) && !empty($params['on_time'])){
            $db->where('on_time','between',[strtotime($params['on_time']),
                strtotime($params['on_time'].' 23:59:59')]);
        }
        if(isset($params['off_time']) && !empty($params['off_time'])){
            $db->where('off_time','between',[strtotime($params['off_time']),
                strtotime($params['off_time'].' 23:59:59')]);
        }

        $data = $db->field('ctime',true)->order('id desc')
            ->paginate($limit)->toArray();


        //判断到期时间以及处理数据
        foreach ($data['data'] as $k => $v){
            $v['on_time'] = date('Y-m-d', $v['on_time']);
            if($v['off_time'] == 0){
                $v['off_time'] = '暂无退役时间';
            }else{
                $v['off_time'] = date('Y-m-d', $v['off_time']);
            }

            $data['data'][$k] = $v;
        }

        return $data;
    }

    /**
     * @name 添加
     * @param $addData
     * @return int|string
     * @author wx
     * @date 2019/10/23 15:34
     */
    function add($addData)
    {
        return db('host_software')->insert($addData);
    }

    function findData($condition, $field = '*')
    {
        return db('host_software')->field($field)->where($condition)->find();
    }

    /**
     * @name 修改
     * @param $condition
     * @param $updateData
     * @return int|string
     * @author wx
     * @date 2019/10/23 15:36
     */
    function modify($condition, $updateData)
    {
        return db('host_software')->where($condition)->update($updateData);
    }

    /**
     * @name 删除
     * @param $condition
     * @return int
     * @author wx
     * @date time
     */
    function del($condition)
    {
        return db('host_software')->where($condition)->delete();
    }

    /**
     * @name: 展示业务系统
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-04-01 13:26:42
     */
    function showSoftware ($id_a = [], $field = '', $index = '') 
    {
        $server = db('HostSoftware')->where('status',1);

        empty($field) ? : $server->field($field);

        empty($id_a) ? : $server->whereIn('id', $id_a);

        $softwareData = empty($index) ? $server->select() : $server->column($field, $index);
        
        return $softwareData;
    
    }
}